layui.use(['admin', 'table', 'form', 'tool', 'xmSelect'], function () {
    let $ = layui.$
        , admin = layui.admin
        , setter = layui.setter
        , view = layui.view
        , table = layui.table
        , form = layui.form
        , tool = layui.tool
        , controller = '/admin/manageData.index'; // 响应成功字段名;

    form.render(null, 'manageDataPage');

    form.on('switch(manageDataStatusSwitch)', function(data){
        let id = $(data.elem).attr("data-id");
        let status = 0;
        if(data.elem.checked){
            status = 1;
        }
        let field = {"id": id, "status": status};
        admin.req({
            url: setter.request.domainName + controller + '/editStatus' //实际使用请改成服务端真实接口
            , data: field
            , type: 'POST'
            , loading: false // 是否开启加载框
            , done: function (res) {

            }
        })
    });

    // 管理员管理
    table.render({
        elem: '#manageDataTable'
        , url: setter.request.domainName + controller + '/index?server=true' //模拟接口
        , toolbar: "#manageDataToolBar"
        , defaultToolbar: setter.defaultToolbar
        , page: true
        , cols: [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'id', title: 'ID', width: 120, sort: true}
			, {field: 'order_number', width: 220, title: '订单编号'}
            , {field: 'name', width: 150, title: '名称'}
            , {field: 'shop_name', width: 180, title: '店铺名称'}
            , {field: 'produce_time', width: 120, title: '生产日期'}
            , {field: 'expire_time', width: 120, title: '到期日期'}
			, {field: 'purchase_time', width: 160, title: '购买时间'}
            , {field: 'line', width: 90, title: '生产线'}
            , {field: 'production_address', width: 100, title: '生产地'}
			, {field: 'source', width: 100, title: '来源',
				templet: function(d) {
					if(d.source == 1){
						return '淘宝';
					}else if(d.source == 2){
						return '京东';
					}else if(d.source == 3){
						return '抖音';
					}else if(d.source == 4){
						return '拼多多';
					}
					return '';
				}
			}
            , {field: 'status', title: '状态', width: 120, align: 'center', templet: "#manageDataStatus"}
            , {field: 'create_time', width: 180, title: '创建时间', sort: true,
                templet: function(d) {
                    return tool.timestampToTime(d.create_time);
                }
            }
            , {title: '操作', align: 'center', fixed: 'right', toolbar: '#manageDataTableTool'}
        ]]
        , parseData: function(res){
            let code = res.code === 1 ? 0 : res.code;
            return {
                "code": code, // 解析接口状态
                "msg": res.msg, // 解析提示文本
                "count": res.data.total, // 解析数据长度
                "data": res.data.list // 解析数据列表
            };
        }
        , text: {none: '暂无数据！'}
    });

    // 自定义工具栏
    table.on('toolbar(manageDataTable)', function (obj) {
        let type = obj.event;
        active[type] ? active[type].call(this) : '';
    })

    // 工具条
    table.on('tool(manageDataTable)', function (obj) {
        let data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('确定删除此数据？', function (index) {
                let ids = [];
				ids.push(obj.data.id);
                let field = {ids: ids};
                // 获取角色信息
                admin.req({
                    url: setter.request.domainName + controller + '/del'
                    , data: field
                    , type: 'delete'
                    , loading: false // 是否开启加载框
                    , done: function () {
                        obj.del();
                        admin.message({
                            title: '删除成功'
                            ,offset: '15px'
                            ,icon: 1
                            ,time: 1500
                            ,callback: function (){}
                        })
                    }
                })
                layer.close(index);
            });
        } else if (obj.event === 'edit') {
            admin.popup({
                title: '编辑数据'
                , area: ['600px', '900px']
                , id: 'manage-data-popup-edit'
                , success: function (layero, index) {
                    let layId = this.id;
                    let field = {id: data.id};
                    // 获取角色信息
                    admin.req({
                        url: setter.request.domainName + controller + '/edit'
                        , data: field
                        , type: 'get'
                        , loading: false // 是否开启加载框
                        , done: function (res) {
                            let initData = res.data.row;
                            initData['lay_index'] = index;
                            view(layId).render('manageData/index/edit', initData).done(function () {
                                form.render(null, 'managePopup');
                            });
                        }
                    })
                }
            });
        }
    });

    //开启搜索
    form.on('submit(manageDataSearch)', function(data){
        let search = [];
        let searchArr = [];
        let nodeList = $(".manageDataSearchForm").find(".searchField");
        nodeList.each(function (i, e) {
            let searchData = {};
            let objNode = $(e);
            let val = objNode.val();
            if(!val && val !== 0  && val !== '0'){
                return true;
            }
            searchData['field'] = objNode.attr("name");
            searchData['val'] = val;
            searchData['operator'] = objNode.attr("operator");
            searchData['render'] = objNode.attr("render");
            search.push(searchData);
            searchArr['search'] = search;
        })
        // 执行重载
        table.reload('manageDataTable', {
            where: searchArr
        });
    });

    let active = {
        add: function() {
            admin.popup({
                title: '添加数据'
                ,area: ['600px', '900px']
                ,id: 'manage-data-popup-add'
                ,success: function(layero, index){
                    view(this.id).render('manageData/Index/add', {lay_index : index}).done(function(){
                        // 渲染表单
                        form.render(null, 'managePopup');
                    });
                }
            });
        }
        ,batchDel: function() {
            let checkStatus = table.checkStatus('manageDataTable')
                , checkData = checkStatus.data // 得到选中的数据
                , ids = []; // ID

            if(checkData.length === 0){
                return layer.msg('请选择要删除的数据');
            }
            $.each(checkData, function (i, val){
                ids.push(val.id);
            })
            layer.confirm('确定删除选中的数据？', function (index) {
                let field = {ids: ids};
                // 获取角色信息
                admin.req({
                    url: setter.request.domainName + controller + '/del'
                    , data: field
                    , type: 'delete'
                    , loading: false // 是否开启加载框
                    , done: function () {
                        table.reload('manageDataTable');
                        admin.message({
                            title: '删除成功'
                            ,offset: '15px'
                            ,icon: 1
                            ,time: 1500
                            ,callback: function (){}
                        })
                    }
                })
                layer.close(index);
            });
        }
    }
})
